from marshmallow import fields
from webutils.tornado.web import RequestHandler
from webutils.tornado.responses import Response
from webutils.webargs.tornado import use_args

from message_broker.services import ChatService

from .. import params


class TextMessageHandler(RequestHandler):
    @use_args({
        'token': params.Token(required=True),
        'limit': fields.Int(required=False),
        'chat_id': fields.Int(required=False),
        'user_id': fields.Int(required=False),
        'search_chat_id': fields.Str(required=False),
        'from': fields.Str(required=False),
        'sort': fields.Str(required=False),
    }, location='query')
    async def get(self, args, unit_id=None):
        chat_service = ChatService(self.connection, room_id=unit_id)
        total = await chat_service.get_text_message_count()

        limit = args.get('limit', 20)
        limit = min(50, limit)
        chat_id = args.get('chat_id', 0)
        # user_id = args.get('user_id', None)
        # search_chat_id = args.get('search_chat_id', None)
        args_from = args.get('from', 'end')
        sort = args.get('sort', 'asc')
        end = chat_id if chat_id > 0 else total
        start = max(end - limit, 0)
        if args_from == 'beginning':
            start = chat_id - 1 if chat_id > 0 else 0
            end = start + limit
            end = total if end >= total else end

        lst = await chat_service.get_text_message_lst(start, end, sort)
        data = {'total': total, 'list': lst}
        resp = Response(errcode=0, errmsg='ok', data=data)
        self.write_response(resp)


class ImageMessageHandler(RequestHandler):
    @use_args({
        'token': params.Token(required=True),
        'limit': fields.Int(required=False),
        'queue_offset': fields.Int(required=False),
        'user_id': fields.Int(required=False),
        'from': fields.Str(required=False),
        'sort': fields.Str(required=False),
    }, location='query')
    async def get(self, args, unit_id=None):
        chat_service = ChatService(self.connection, room_id=unit_id)
        total = await chat_service.get_image_message_count()

        limit = args.get('limit', 20)
        limit = min(50, limit)
        chat_id = args.get('queue_offset', 0)
        # user_id = args.get('user_id', None)
        # search_chat_id = args.get('search_chat_id', None)
        args_from = args.get('from', 'end')
        sort = args.get('sort', 'asc')
        end = chat_id if chat_id > 0 else total
        start = max(end - limit, 0)
        if args_from == 'beginning':
            start = chat_id - 1 if chat_id > 0 else 0
            end = start + limit
            end = total if end >= total else end

        lst = await chat_service.get_image_message_lst(start, end, sort)
        data = {'total': total, 'list': lst}
        resp = Response(errcode=0, errmsg='ok', data=data)
        self.write_response(resp)


class BlacklistHandler(RequestHandler):
    @use_args({
        'token': params.Token(required=True),
    }, location='query')
    async def get(self, args, unit_id=None):
        chat_service = ChatService(self.connection, room_id=unit_id)
        members = await chat_service.get_blocked_users()
        groups = await chat_service.get_blocked_groups()
        data = {
            'groups': list(map(lambda item: item.value, groups.keys())),
            'members': list(members.values()),
        }
        resp = Response(errcode=0, errmsg='ok', data=data)
        self.write_response(resp)
